package com.ksm.domain.dao.document;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ksm.domain.entity.Document;
import com.ksm.domain.entity.DocumentType;

@Repository
public class DocumentDaoImpl implements DocumentCustomDao {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public String getTest() {

		return "Yo man";
	}

	@PostConstruct
	public void init() {
		// @SuppressWarnings("unused")
		// JpaEntityInformation<Document, Long> personEntityInfo = new
		// JpaMetamodelEntityInformation<Document, Long>(Document.class,
		// entityManager.getMetamodel());

	}

	public List<Document> findByFilter(DocumentType type, String interlocuteur) {

		List<Document> documents = new ArrayList<Document>();

		Session session = (Session) entityManager.getDelegate();
		Criteria criteria = session.createCriteria(Document.class);
		if (type != null) {
			criteria.add(Restrictions.eq(Document.COLUMN_DOCUMENT_TYPE, type));
		}
		if (interlocuteur != null) {
			criteria.add(Restrictions.eq(Document.COLUMN_INTERLOCUTEUR, interlocuteur));
		}

		documents = criteria.list();
		return documents;
	}
}
